




$(function () {



        $(".toRes").click(function () {
            $(".res_box").css("display", "block");
            $(".login_box").css("display", "none");
        });
    $(".res_slider").on("mouseenter", function () {

        $(".res_imgBox").css("display", "block");
        $(".res_img").css("display", "block")
    });
    $(".res_slider").on("mouseleave", function () {

        $(".res_imgBox").css("display", "none");
        $(".res_img").css("display", "none")
    });







    var sliderFlag=false;
    $(".res_ver").html(random());
    var phoneFlag=false;
    var emailFlag=false;
    var $FormName=$(".res_form [name=uname]");
    $FormName.on("blur",function () {
        if($FormName.val().indexOf("@")==-1){
            phoneFlag=/^1[34578]\d{9}$/.test($FormName.val());

            $.ajax({
                type:"post",
                url:"http://127.0.0.1/1803/wangyinet/src/server/checkPhone.php",
                data:{"val":$FormName.val()},
                dataType:"json"
            }).then(function (res) {

                if (res.status==0) {
                    $(".msg").text("手机号已存在").css("color", "red")
                    $(".msg_img")[0].style.background = "url('./images/error.png')no-repeat center"
                    phoneFlag = false;
                }
                if(res.status==1){
                    if(phoneFlag){
                        $(".msg").text("手机号验证合法").css("color", "lawngreen");
                        $(".msg_img")[0].style.background = "url('./images/right.png')no-repeat center";
                    }else{
                        $(".msg").text("手机号不合法").css("color", "red");
                        $(".msg_img")[0].style.background = "url('./images/error.png')no-repeat center"
                    }
                }
            })


        }else{
            emailFlag=/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/.test($FormName.val());
            $.ajax({
                type:"post",
                url:"http://127.0.0.1/1803/wangyinet/src/server/checkEmail.php",
                data:{"val":$FormName.val()},
                dataType:"json",
            }).then(function (res) {
                if (res.status == 0) {
                    $(".msg").text("邮箱已存在").css("color", "red")
                    $(".msg_img")[0].style.background = "url('./images/error.png')no-repeat center"
                    emailFlag = false;
                }
                if (res.status == 1) {
                    if(emailFlag){
                        $(".msg").text("邮箱验证合法").css("color", "lawngreen");
                        $(".msg_img")[0].style.background = "url('./images/right.png')no-repeat center";
                    }else{
                        $(".msg").text("邮箱验证不合法").css("color", "red");
                        $(".msg_img")[0].style.background = "url('./images/error.png')no-repeat center"
                    }
                }
            })

        }
    });

    var pwdFlag=false;
    var $FormPwd=$(".res_form [name=upwd]");
    $FormPwd.on("blur",function () {
        pwdFlag=/^\w{8,18}$/.test($FormPwd.val());
        if(pwdFlag){
            $(".msg").text("密码验证合法").css("color", "lawngreen");
            $(".msg_img")[0].style.background = "url('./images/right.png')no-repeat center";
        }else{
            $(".msg").text("密码验证不合法").css("color", "red");
            $(".msg_img")[0].style.background = "url('./images/error.png')no-repeat center"
        }
    })


    $(".res_ver").click(function () {
        $(".res_ver").html(random());
    })

    var verFlag=false;
    var $FormVer=$(".res_form [name=uver]");
    $FormVer.on("blur",function () {
        verFlag=($(".res_ver").html()==$FormVer.val());
        if(verFlag){
            $(".msg").text("验证码正确").css("color", "lawngreen")
            $(".msg_img")[0].style.background = "url('./images/right.png')no-repeat center";
        }else{
            $(".msg").text("验证码不正确").css("color", "red")
            $(".msg_img")[0].style.background = "url('./images/error.png')no-repeat center"
        }
    });


    $(".res_slider").on("mousedown", function (evt) {

        let e = evt || window.event;
        // console.log(this)
        var static_slider = $(this).offset().left;
        // console.log(static_slider);
        // console.log(e.clientX);
        var static_img = $(".res_img").offset().left;
        let disX = e.clientX - $(this).offset().left;

        // console.log(disX);
        var self = this;
        $(document).on("mousemove", function (evt) {
            let e = evt || window.event;
            let movex = e.clientX - disX - 902
            if (movex <= 0) {
                movex = 0
            }
            if (movex > ($(".res_sliderBox").width() - $(".res_slider").width())) {
                movex = $(".res_sliderBox").width() - $(".res_slider").width();
            }
            self.style.left = movex + "px";


            $(".res_img")[0].style.left = movex + "px";



        });
        $(document).on("mouseup", function () {
            aaa=true;
            if (!($(".res_img").position().left < 205 && $(".res_img").position().left > 200)) {

                $(document).off("mousemove mouseup");
                $(".res_slider")[0].style.left = "2px";
                $(".res_img")[0].style.left = "2px";
                $(".msg_img")[0].style.backgroundImage = "url('./images/error.png')";
                $(".msg").text("滑动验证码错误").css("color", "red");
                sliderFlag=true;

            } else {
                console.log(2222);
                sliderFlag=true;
                $(".msg_img")[0].style.background = "url('./images/right.png')no-repeat center";
                $(".msg").text("滑动验证码正确").css("color", "lawngreen");
                $(document).off("mousemove mouseup");
                $(".res_slider").off("mousedown");
            }


            $(".res_form").on("submit",function () {

                console.log($(".check")[0].checked);
                console.log(verFlag,sliderFlag,pwdFlag,$(".check")[0].checked);
                if(phoneFlag){
                    if(verFlag&&pwdFlag&&sliderFlag&&$(".check")[0].checked){
                        console.log(222)
                        $.ajax({
                            type:"post",
                            url:"http://localhost/1803/wangyinet/src/server/pregister.php",
                            data:{"uname":$FormName.val(),"upwd":$FormPwd.val()},
                            dataType:"json"
                        }).then(function (res) {
                            if(res.status==1){
                                alert(res.msg)
                            }
                        })
                    }
                }
                if(emailFlag){
                    if(verFlag&&pwdFlag&&sliderFlag&&$(".check")[0].checked){
                        $.ajax({

                        })
                    }
                }
                return false;
            });


        });
        // console.log(sliderFlag);
    });








});
function random(){
    var str="";
    for(var i=0;i<4;i++){
        if(Math.random()>0.5){
            str+=Math.floor(Math.random()*10);
        }else{
            str+=String.fromCharCode(Math.floor(Math.random()*26)+65);
        }
    }
    return str
}
